/* Copyright Andy Mitchell 2007. No warranties */

function ContactFilterPopuper(gtdinboxPage, elBody){
	this._eventObservers = new EventObservers(this);
	this._gtdinboxPage = gtdinboxPage;
	this._doc = gtdinboxPage.doc;
	
	this._eventObservers.observe( elBody, "contextmenu", this.contextMenu, true );
	this._eventObservers.observe( this._doc.body, "mouseup", this.mouseDown, true );
	this._eventObservers.observe( this._doc.body, "contextmenu", this.stopEvent, false );
	
};

ContactFilterPopuper.prototype.createPopup = function(name, email, el){
	if( this._filterChild ){
		this._filterChild.destroy();
		this._filterChild = null;
	}
	var f = this._gtdinboxPage.gtdinboxBrowser.settings.filters["Contacts"];
	var search = "";
	if( email ) search = "from:" + email + " OR to:" + email;
	if( name ) search += (search? " OR " : "") + "from:\"" + name + "\" OR to:\"" + name + "\"";
	this._filterChild = new FilterPopupUI( this._gtdinboxPage ).create(el.ownerDocument.body, "(" + search + ")", true, false, f.inlineTypes, f.extendTypes);
	this._filterChild.popup(el,{location:'right'});
};

ContactFilterPopuper.prototype.viewChange = function(gmail){
	if( this._elContactsBody ) return;
	
	var w = this._gtdinboxPage.window.frames[1];
	for( var i = 0; i < w.frames.length; i++ ){
		if( w.frames[i].location.href=="about:blank" ){
			var doc = w.frames[i].document;
			w.frames[i].wrappedJSObject.onload = GIPROTOTYPE.bind(this, function(){
				if( this._elContactsBody ) return;
				this._elContactsBody = this._gtdinboxPage.gmailDOM.element("contacts-frame-body");
				if( this._elContactsBody ) this._eventObservers.observe( this._elContactsBody, "contextmenu", this.contextMenu, true );
				this._gtdinboxPage.addCSS(this._elContactsBody.ownerDocument);
			});
		}
	}	
	
};

ContactFilterPopuper.prototype.contextMenu = function(event){
	this._processClick(event);
};
ContactFilterPopuper.prototype.mouseDown = function(event){
	if( event.button!=2 ) return;
	this._processClick(event);
};
ContactFilterPopuper.prototype.stopEvent = function(event){
try{
	if( this._extractNameEmail( event, GIPROTOTYPE.element(event) ) ){
		GIPROTOTYPE.stop(event);
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("contactfilter stopevent\n" + e.toString());
}
};

ContactFilterPopuper.prototype._processClick = function(event){
try{	
	var el = GIPROTOTYPE.element(event);
	var contact = this._extractNameEmail(event, el);
	if( contact ){
		GIPROTOTYPE.stop(event);
		this.createPopup(contact[0], contact[1], el);
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("contactfilterpopuper mousedown\n" + e.toString());
}
};


ContactFilterPopuper.prototype._extractNameEmail = function(event, el){
	var name = "";
	var email = "";
	if( email = el.getAttribute("email") ){
	}else if( el.className=="HHshnc" && el.nodeName=="SPAN" && el.parentNode.className=="Gqb6V" ){	// contacts box
		name = el.innerHTML;
	}else if( el.className=="QO5lqe nRrh7" && el.nodeName=="DIV" && el.parentNode.nodeName=="TD" ){	// chat
		name = el.innerHTML;
	}else if( el.className=="text" && el.parentNode.className.indexOf("row")>-1 && el.previousSibling && el.previousSibling.className=="check" ){ // real contacts
		name = el.innerHTML;
		email = el.title;
	}
	if( name || email ){
		this._gtdinboxPage.unsafeWindow.console.trace();
		return [name, email];
	}
};

ContactFilterPopuper.prototype.destroy = function(){
	this._eventObservers.destroy();
		
	if( this._filterChild ){
		this._filterChild.destroy();
		this._filterChild = null;
	}
};